"""
The trading dates
"""
__all__ = ( "TXDates" )

from datetime import datetime
from dateutil.rrule import rrule, DAILY


EXCLUDED_DATES = [
    
    # 2010
    '2010-02-15', '2010-02-16', '2010-02-17',
    '2010-02-18', '2010-02-19', '2010-04-05',
    '2010-05-03', '2010-06-14', '2010-06-15',
    '2010-06-16', '2010-09-22', '2010-09-23',
    '2010-09-24', '2010-10-01', '2010-10-02',
    '2010-10-03', '2010-10-04', '2010-10-05',
    '2010-10-06', '2010-10-07',

    # 2011
    '2011-01-03', '2011-02-02', '2011-02-03',
    '2011-02-04', '2011-02-07', '2011-02-08',
    '2011-04-04', '2011-04-05', '2011-05-02',
    '2011-06-06', '2011-09-12', '2011-10-03',
    '2011-10-04', '2011-10-05', '2011-10-06',
    '2011-10-07',

    # 2012
    '2012-01-02', '2012-01-03', '2012-01-23',
    '2012-01-24', '2012-01-25', '2012-01-26',
    '2012-01-27', '2012-04-02', '2012-04-03',
    '2012-04-04', '2012-04-30', '2012-05-01', 
    '2012-06-22',
    '2012-06-25', '2012-10-01', '2012-10-02', 
    '2012-10-03', '2012-10-04', '2012-10-05',

    # 2013
    '2013-01-01', '2013-01-02', '2013-01-03', 
    '2013-02-11', '2013-02-12', '2013-02-13',
    '2013-02-14', '2013-02-15', '2013-04-04',
    '2013-04-05', '2013-04-29', '2013-04-30',
    '2013-05-01', '2013-06-10', '2013-06-11',
    '2013-06-12', '2013-09-19', '2013-09-20',
    '2013-10-01', '2013-10-02', '2013-10-03',
    '2013-10-04', '2013-10-07',

    # 2014
    '2014-01-01', '2014-01-31', '2014-02-03',
    '2014-02-04', '2014-02-05', '2014-02-06',
    '2014-04-07', '2014-05-01', '2014-05-02',
    '2014-06-02', '2014-09-08', '2014-10-01',
    '2014-10-02', '2014-10-03', '2014-10-06',
    '2014-10-07',

    # 2015
    '2015-01-01', '2015-01-02',
    '2015-02-18', '2015-02-18', '2015-02-19',
    '2015-02-20', '2015-02-23', '2015-02-24',
    '2015-04-06', '2015-05-01', '2015-06-22',
    '2015-09-03', '2015-09-04',
    '2015-10-01', '2015-10-02', '2015-10-05',
    '2015-10-06', '2015-10-07',

    # 2016
    '2016-01-01', '2016-02-08', '2016-02-09',
    '2016-02-10', '2016-02-11', '2016-02-12',
    '2016-04-04', '2016-05-02',
    '2016-06-09', '2016-06-10',
    '2016-09-15', '2016-09-16',
    '2016-10-03', '2016-10-04', '2016-10-05',
    '2016-10-06', '2016-10-07'
]

class TXDates(object):
    """
    >>> dates = TXDates.between(start, end)
    >>> for date in dates:
            print date
    """
    @classmethod
    def between(cls, start, end):
        return cls(start, end)

    def __init__(self, start, end):
        self.dates = None
        self._between(start, end)

    def __iter__(self):
        return self.dates

    def _between(self, start, end):
        start_date = datetime(*[ int(s) for s in start.split("-")])
        end_date = datetime(*[ int(s) for s in end.split("-")])
        T = lambda d: d.date().isoformat()
        # from Monday to Friday
        weekdays = [0, 1, 2, 3, 4]
        period_dates = (
            d.date().isoformat() 
            for d in rrule(
                freq=DAILY, 
                dtstart=start_date, 
                until=end_date, 
                byweekday=weekdays
            )
        )
        self.dates = ( 
            date for date in period_dates if date not in EXCLUDED_DATES
        )
